﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework5
{
    class OrderService
    {
        private List<Order> list = new List<Order>();

        public OrderService(List<Order> list)
        {
            this.list = list;
        }

        //按订单号排序
        public void Sort()
        {
            list.Sort((o1, o2) => o1.Num.CompareTo(o2.Num));
        }
        //其他排序
        public void Sort(Comparison<Order> comparison)
        {
            list.Sort(comparison);
        }
        //按订单号查询
        public Order SearchByNum(int n)
        {
            var query = list.Where(order => order.Num == n).OrderBy(order=>order.Details.Money).FirstOrDefault();
            return query;
        }
        //按商品名称查询
        public Order SearchByGoods(string item)
        {
            var query = list.Where(order => order.Details.Good.Name==item).OrderBy(order => order.Details.Money).FirstOrDefault();
            return query;
        }
        //按客户查询
        public Order SearchByCustomer(string name)
        {
            var query = list.Where(order => order.Customer.Name == name).OrderBy(order => order.Details.Money).FirstOrDefault();
            return query;
        }
        //按订单金额查询
        public Order SearchByMoney(double money)
        {
            var query = list.Where(order => order.Details.Money == money).OrderBy(order => order.Details.Money).FirstOrDefault();
            return query;
        }
        //删除订单
        public void Delete(int num)
        {
                Order o = list.Where(order => order.Num == num).FirstOrDefault();
                if(o!=null)
                    list.Remove(o);
                else
                    throw new Exception("未找到订单");
        }
        //修改订单顾客
        public void Amend(Order order,Customer customer)
        {
            order.Customer = customer;
        }
        //修改订单序号
        public void Amend(Order order, int num)
        {
            order.Num = num;
        }
        //修改订单细节
        public void Amend(Order order, OrderDetails od)
        {
            order.Details=od;
        }
        //添加订单
        public void Add(Order order)
        {
            if (list.Contains(order))
            {
                throw new Exception("此订单已存在");
            }
            else
                list.Add(order);
        }
        //打印订单
        public void Print()
        {
            foreach(Order order in list)
            {
                Console.WriteLine(order.ToString());
            }
        }
    }
}
